﻿using System.Collections.Generic;
using System.Data;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.Services.Protocols;

namespace EazyBPMS.Web.webservices
{
    /// <summary>
    /// WebService1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
    [System.Web.Script.Services.ScriptService]
    [SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]
    public class WebService1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod]
        public int DoMulti(int num1, int num2)
        {
            int result;
            result = num1 * num2;
            return result;
        }
        [WebMethod]
        public void getSiteAll(string userkey)
        {
            Context.Response.ContentType = "text/html";
            Context.Response.Charset = "UTF-8"; //设置字符集类型1
            Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            msgmodel msgmodel = new msgmodel();
            if (userkey == "68c62a05d5da7e0001e6e402e59c65df")
            {
                string sql = "";
                DataTable dt;
                sql = "select * from fx_site ORDER BY SITE_CODE";
                dt = DbHelperSQL.ExecuteDataSet(sql);

                List<sitemodel> sitelist = new List<sitemodel>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    sitemodel sitemodel = new sitemodel();
                    sitemodel.sitecode = dt.Rows[i]["SITE_CODE"].ToString();
                    sitemodel.sitename = dt.Rows[i]["SITE_NAME"].ToString();
                    sitemodel.coordinates = dt.Rows[i]["LONGITUDE"].ToString() + "," + dt.Rows[i]["LATITUDE"].ToString();
                    sitemodel.datetime = dt.Rows[i]["MODIFIED_TIME"].ToString();
                    sitelist.Add(sitemodel);
                }
                msgmodel.sitelist = sitelist;
                msgmodel.state = "success";
            }
            else
            {
                msgmodel.state = "userkey错误！";
            }
            Context.Response.Write(new JavaScriptSerializer().Serialize(msgmodel));
        }
        [WebMethod]
        public void getStieAllData(string userkey)
        {
            Context.Response.ContentType = "text/html";
            Context.Response.Charset = "UTF-8"; //设置字符集类型1
            Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            msgmodel2 msgmodel = new msgmodel2();
            if (userkey == "68c62a05d5da7e0001e6e402e59c65df")
            {
                string sql = "";
                DataTable dt;
                sql = "select * from alldata where site_code is not null ORDER BY site_code";
                dt = DbHelperSQL.ExecuteDataSet(sql);

                List<sitemodel2> sitelist = new List<sitemodel2>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    sitemodel2 sitemodel = new sitemodel2();
                    sitemodel.sitecode = dt.Rows[i]["SITE_CODE"].ToString();
                    sitemodel.sitename = dt.Rows[i]["SITE_NAME"].ToString();
                    sitemodel.data1 = dt.Rows[i]["water_flow"].ToString();
                    sitemodel.data2 = dt.Rows[i]["rain_flow"].ToString();
                    sitemodel.data3 = dt.Rows[i]["liquid_value"].ToString();
                    sitemodel.datetime1 = dt.Rows[i]["time1"].ToString();
                    sitemodel.datetime2 = dt.Rows[i]["time2"].ToString();
                    sitemodel.datetime3 = dt.Rows[i]["time3"].ToString();
                    sitemodel.today1 = dt.Rows[i]["today1"].ToString();
                    sitemodel.sum1 = dt.Rows[i]["sum1"].ToString();
                    sitelist.Add(sitemodel);
                }
                msgmodel.sitelist = sitelist;
                msgmodel.state = "success";
            }
            else
            {
                msgmodel.state = "userkey错误！";
            }
            Context.Response.Write(new JavaScriptSerializer().Serialize(msgmodel));
        }
        [WebMethod]
        public void getSiteOne(string userkey,string sitecode)
        {
            //1112

            Context.Response.ContentType = "text/html";
            Context.Response.Charset = "UTF-8"; //设置字符集类型1231
            Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            msgmodel3 msgmodel = new msgmodel3();
            if (userkey == "68c62a05d5da7e0001e6e402e59c65df")
            {
                string sql = "";
                DataTable dt;
                sql = "select * from alldata where site_code='"+ sitecode + "'";
                dt = DbHelperSQL.ExecuteDataSet(sql);
                sitemodel3 sitemodel = new sitemodel3();
                if (dt.Rows.Count > 0)
                {
                    int i = 0;
                    sitemodel.sitecode = dt.Rows[i]["SITE_CODE"].ToString();
                    sitemodel.sitename = dt.Rows[i]["SITE_NAME"].ToString();
                    sitemodel.data1 = dt.Rows[i]["water_flow"].ToString();
                    sitemodel.data2 = dt.Rows[i]["rain_flow"].ToString();
                    sitemodel.data3 = dt.Rows[i]["liquid_value"].ToString();
                    sitemodel.datetime1 = dt.Rows[i]["time1"].ToString();
                    sitemodel.datetime2 = dt.Rows[i]["time2"].ToString();
                    sitemodel.datetime3 = dt.Rows[i]["time3"].ToString();
                    sitemodel.today1 = dt.Rows[i]["today1"].ToString();
                    sitemodel.sum1 = dt.Rows[i]["sum1"].ToString();
                }
                //pw
                sql = "select * from fx_DEVICE where site_code='" + sitecode + "' and DEVICE_TYPE='2'";
                dt = DbHelperSQL.ExecuteDataSet(sql);
                List<datalist1> datalist1 = new List<datalist1>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    datalist1 datalistmodel1 = new datalist1();
                    datalistmodel1.dataname= dt.Rows[i]["DEVICE_NAME"].ToString();
                    datalistmodel1.data1 = dt.Rows[i]["IS_DELETE"].ToString();
                    datalistmodel1.data2 = dt.Rows[i]["IS_LAST_TASK"].ToString();
                    datalistmodel1.data3 = dt.Rows[i]["DEVICE_EFFECT"].ToString();
                    var DEVICE_CODE= dt.Rows[i]["DEVICE_CODE"].ToString();
                    sql = "select top 1 * from fx_DEVICE_POWER where DEVICE_CODE='"+ DEVICE_CODE + "' order by CREATED_TIME desc";
                    dt = DbHelperSQL.ExecuteDataSet(sql);
                    datalistmodel1.data4 = dt.Rows[i]["ELECTRICITY"].ToString();
                    datalistmodel1.data5 = dt.Rows[i]["VOLTAGE"].ToString();
                    datalist1.Add(datalistmodel1);
                }
                //jw
                sql = "select * from fx_DEVICE where site_code='" + sitecode + "' and DEVICE_TYPE='9'";
                dt = DbHelperSQL.ExecuteDataSet(sql);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    datalist1 datalistmodel1 = new datalist1();
                    datalistmodel1.dataname = dt.Rows[i]["DEVICE_NAME"].ToString();
                    datalistmodel1.data1 = dt.Rows[i]["IS_DELETE"].ToString();
                    datalistmodel1.data2 = dt.Rows[i]["IS_LAST_TASK"].ToString();
                    datalistmodel1.data3 = dt.Rows[i]["DEVICE_EFFECT"].ToString();
                    var DEVICE_CODE = dt.Rows[i]["DEVICE_CODE"].ToString();
                    sql = "select top 1 * from fx_DEVICE_POWER where DEVICE_CODE='" + DEVICE_CODE + "' order by CREATED_TIME desc";
                    dt = DbHelperSQL.ExecuteDataSet(sql);
                    datalistmodel1.data4 = dt.Rows[i]["ELECTRICITY"].ToString();
                    datalistmodel1.data5 = dt.Rows[i]["VOLTAGE"].ToString();
                    datalist1.Add(datalistmodel1);
                }
                sitemodel.datalist1 = datalist1;
                msgmodel.sitemodel = sitemodel;
            }
            else
            {
                msgmodel.state = "userkey错误！";
            }
            Context.Response.Write(new JavaScriptSerializer().Serialize(msgmodel));
        }
        [WebMethod]
        public void getStieOneHisDataToday(string userkey,string siteid,string type)
        {
            Context.Response.ContentType = "text/html";
            Context.Response.Charset = "UTF-8"; //设置字符集类型1
            Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            msgmodel msgmodel = new msgmodel();
            if (userkey == "68c62a05d5da7e0001e6e402e59c65df")
            {
                string sql = "";
                DataTable dt;
                List<sitemodel> sitelist = new List<sitemodel>();
                sitemodel sitemodel = new sitemodel();
                List<datamodel> datalist = new List<datamodel>();
                if (type == "1")
                {
                    sql = "select * from fx_water where site_code ='" + siteid + "' ORDER BY created_time desc";
                    dt = DbHelperSQL.ExecuteDataSet(sql);
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        datamodel datamodel = new datamodel();
                        datamodel.name = "流量";
                        datamodel.data= dt.Rows[i]["water_flow"].ToString();
                        datamodel.time = dt.Rows[i]["created_time"].ToString();
                        datalist.Add(datamodel);
                    }
                    sitemodel.datalist = datalist;
                    sitelist.Add(sitemodel);
                    msgmodel.sitelist = sitelist;
                }
                else if (type == "2")
                {
                    sql = "select * from fx_rain where site_code ='" + siteid + "' ORDER BY created_time desc";
                    dt = DbHelperSQL.ExecuteDataSet(sql);
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        datamodel datamodel = new datamodel();
                        datamodel.name = "雨量";
                        datamodel.data = dt.Rows[i]["rain_flow"].ToString();
                        datamodel.time = dt.Rows[i]["created_time"].ToString();
                        datalist.Add(datamodel);
                    }
                    sitemodel.datalist = datalist;
                    sitelist.Add(sitemodel);
                    msgmodel.sitelist = sitelist;
                }
                else if (type == "3")
                {
                    sql = "select * from fx_liquid where site_code ='" + siteid + "' ORDER BY created_time desc";
                    dt = DbHelperSQL.ExecuteDataSet(sql);
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        datamodel datamodel = new datamodel();
                        datamodel.name = "液位";
                        datamodel.data = dt.Rows[i]["liquid_value"].ToString();
                        datamodel.time = dt.Rows[i]["created_time"].ToString();
                        datalist.Add(datamodel);
                    }
                    sitemodel.datalist = datalist;
                    sitelist.Add(sitemodel);
                    msgmodel.sitelist = sitelist;
                }
               
            }
            else
            {
                msgmodel.state = "userkey错误！";
            }
            Context.Response.Write(new JavaScriptSerializer().Serialize(msgmodel));
        }
    }
}
